
//
//  FZEmotionView.swift
//  MyWeiBo
//
//  Created by fangzhengwei on 2017/4/6.
//  Copyright © 2017年 fangzhengwei. All rights reserved.
//

import UIKit
import SnapKit
class FZEmotionView: UIView {
    //下面toolBar
    fileprivate lazy var emotionStackView : FZEmotionToolBar = FZEmotionToolBar()
    //放置表情的collection
    lazy var emotionCollectionView : FZEmotionCollection = FZEmotionCollection()
    
    fileprivate lazy var emotionPageControl : UIPageControl = {
        
        let pageControl =  UIPageControl()
        pageControl.hidesForSinglePage = true
        pageControl.setValue(UIImage(named: "compose_keyboard_dot_normal")!, forKey: "_pageImage")
        pageControl.setValue(UIImage(named: "compose_keyboard_dot_selected")!, forKey: "_currentPageImage")
        return pageControl
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    fileprivate var indexPath : IndexPath?
    
    override func layoutSubviews() {
        let layout = self.emotionCollectionView.collectionViewLayout as! UICollectionViewFlowLayout
        
        layout.itemSize = self.emotionCollectionView.bounds.size
        
     }
        required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setIndexPathData(indexPath : IndexPath) {
        
        self.emotionPageControl.numberOfPages = (FZEmotionViewModel.sharedEmotion.allEmotionArray?[indexPath.section].count) ?? 0
        self.emotionPageControl.currentPage = indexPath.item
        
    }
    
    func reloadRecentCollection() {
        
        
        emotionCollectionView.reloadItems(at: [IndexPath(item: 0, section: 0)])
    }

   fileprivate func setupUI() {
    
      addSubview(emotionStackView)
     addSubview(emotionCollectionView)
     addSubview(emotionPageControl)
    //默认滚动到第一组
    setIndexPathData(indexPath:IndexPath(item: 0, section: 1))
    
    emotionStackView.snp.makeConstraints { (make) in
        make.left.right.bottom.equalTo(self)
        make.height.equalTo(35)
    }
    emotionCollectionView.snp.makeConstraints { (make) in
         make.top.left.right.equalTo(self)
         make.bottom.equalTo(emotionStackView.snp.top)
    }
    emotionPageControl.snp.makeConstraints { (make) in
        make.centerX.equalTo(emotionCollectionView)
        make.bottom.equalTo(emotionCollectionView)
        make.height.equalTo(10)
    }

    emotionCollectionView.scrollClosure = { [weak self] (indexPath : IndexPath)->() in
       
        self?.emotionStackView.scrollToSelctedButton(section: indexPath.section)
        self?.setIndexPathData(indexPath: indexPath)
    }
    
    emotionStackView.emotionClosure = { [weak self] (btnType)->() in
        
        switch btnType {
        case .recent:
             print("最近")
             self?.indexPath = IndexPath(item: 0, section: 0)
        case .defaut:
            print("默认")
            self?.indexPath = IndexPath(item: 0, section: 1)
        case .emoji:
            print("emoji")
            self?.indexPath = IndexPath(item: 0, section: 2)
        case .flower:
            print("浪小花")
            self?.indexPath = IndexPath(item: 0, section: 3)
        
            
     }
       self?.emotionCollectionView.scrollToItem(at: (self?.indexPath)!, at: [], animated: false)
    
   }
    
 }

}

class FZEmotionCollection: UICollectionView ,UICollectionViewDelegate,UICollectionViewDataSource {
    
    var sectionItemClosure : ((Int , Int)->())?
    var scrollClosure : ((IndexPath)->())?
    //获取数据的viewmodel
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        
        let layout = UICollectionViewFlowLayout()
        super.init(frame: frame, collectionViewLayout: layout)
        
        layout.scrollDirection = .horizontal
        self.backgroundColor = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)
        self.isPagingEnabled = true
        //  隐藏滚动指示器
        self.showsVerticalScrollIndicator = false
        self.showsHorizontalScrollIndicator = false
        
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        //  去掉弹簧效果
        self.bounces = false
        self.delegate = self
        self.dataSource = self
        //特别重要
       setupUI()
    }

    func setupUI() {

    DispatchQueue.main.async {
            
        self.contentOffset = CGPoint(x: screenWidth, y: 0)
        
    }

    register(FZEmotionCollCell.self, forCellWithReuseIdentifier: emotionCollectionCell)
        
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension FZEmotionCollection {
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        
        return FZEmotionViewModel.sharedEmotion.allEmotionArray?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return FZEmotionViewModel.sharedEmotion.allEmotionArray?[section].count ?? 0
     
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell =  collectionView.dequeueReusableCell(withReuseIdentifier: emotionCollectionCell, for: indexPath) as! FZEmotionCollCell
        
//        cell.backgroundColor = UIColor(red: CGFloat(arc4random_uniform(256)) / 255.0, green: CGFloat(arc4random_uniform(256)) / 255.0, blue: CGFloat(arc4random_uniform(256)) / 255.0, alpha: 1)
        
        cell.emotionArray = FZEmotionViewModel.sharedEmotion.allEmotionArray?[indexPath.section][indexPath.item]
        
        return cell
        
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        
        let offsetX = scrollView.contentOffset.x + scrollView.bounds.size.width * 0.5
        
        if let indexPath = self.indexPathForItem(at: CGPoint(x: offsetX, y: scrollView.bounds.height * 0.5)) {
            
            scrollClosure?(indexPath)
            
        }
        
        
    }
    
}












